home *** CD-ROM | disk | FTP | other *** search
/ APDL Other Worlds / APDL Other Worlds Collection.iso / SF3000 / Extras / CBlibrary / c / FednetComp < prev    next >
Encoding:
Text File  |  2003-10-23  |  4.4 KB  |  159 lines

  1. /*
  2.  * CBLibrary - FednetComp
  3.  * Copyright (C) 2003  Chris Bazley
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2.1 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public
  16.  * License along with this library; if not, write to the Free Software
  17.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  */
  19.  
  20. /* Veneer to Fednet compression modules */
  21.  
  22. /* ANSI library headers */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. /* RISC OS library headers */
  28. #include "kernel.h"
  29. #include "flex.h"
  30.  
  31. /* Other headers */
  32. #include "Macros.h"
  33. #include "msgtrans.h"
  34. #include "hourglass.h"
  35. #include "timer.h"
  36. #include "FednetComp.h"
  37.  
  38. extern _kernel_oserror shared_err_block;
  39.  
  40. /* ----------------------------------------------------------------------- */
  41. /*                         Public functions                                */
  42.  
  43. _kernel_oserror *load_compressed(char *filepath, flex_ptr buffer_anchor)
  44. {
  45.   /* Allocate buffer and load compressed Fednet datafile */
  46.  
  47.   _kernel_last_oserror(); /* reset SCL's error recording */
  48.  
  49.   /* Get (decompressed) memory requirements */
  50.   FILE *readfile = fopen(filepath, "r");
  51.   if(readfile == NULL) {
  52.     THROW(_kernel_last_oserror()) /* any OS error? */
  53.     WRITE_ERR_SUB1(shared_err_block, "OpenInFail", filepath);
  54.     return &shared_err_block; /* fail */
  55.   }
  56.  
  57.   int buffer_size;
  58.   if(fread(&buffer_size, sizeof(int), 1, readfile) != 1) {
  59.     fclose(readfile);
  60.     THROW(_kernel_last_oserror()) /* any OS error? */
  61.     WRITE_ERR_SUB1(shared_err_block, "ReadFail", filepath);
  62.     return &shared_err_block; /* fail */
  63.   }
  64.   fclose(readfile);
  65.  
  66.   /* Allocate buffer for data */
  67.   if(!flex_alloc(buffer_anchor, buffer_size)) {
  68.     WRITE_GERR(shared_err_block, "NoMem");
  69.     return &shared_err_block; /* fail */
  70.   }
  71.  
  72.   /* Construct CLI command */
  73. #ifndef OLD_SCL_STUBS
  74.   char command[6+strlen(filepath)+2+8+1];
  75. #else
  76.   char *command = malloc(6+strlen(filepath)+2+8+1);
  77.   if(command == NULL) {
  78.     WRITE_GERR(shared_err_block, "NoMem");
  79.     return &shared_err_block; /* fail */
  80.   }
  81. #endif
  82.  
  83.   int old_budge = flex_set_budge(0); /* prevent budge */
  84.  
  85. #ifndef OLD_SCL_STUBS
  86.   snprintf(command, sizeof(command), "Cload %s &%X", filepath, (int)*buffer_anchor);
  87. #else
  88.   sprintf(command, "Cload %s &%X", filepath, (int)*buffer_anchor);
  89. #endif
  90.  
  91.   /* Decompress file */
  92.   hourglass_on();
  93.   int err = _kernel_oscli(command);
  94.   hourglass_off();
  95.  
  96. #ifdef OLD_SCL_STUBS
  97.   free(command);
  98. #endif
  99.  
  100.   flex_set_budge(old_budge); /* allow budge */
  101.  
  102.   if(err == _kernel_ERROR) {
  103.     flex_free(buffer_anchor);
  104.     return _kernel_last_oserror(); /* fail */
  105.   }
  106.   return NULL; /* success */
  107. }
  108.  
  109. /* ----------------------------------------------------------------------- */
  110.  
  111. _kernel_oserror *save_compressed(char *filepath, int filetype, flex_ptr buffer_anchor)
  112. {
  113.   /* Save the specified memory as a compressed Fednet file */
  114.  
  115.   /* Construct CLI command */
  116.   {
  117. #ifndef OLD_SCL_STUBS
  118.     char command[6+strlen(filepath)+2+8+2+8+1];
  119. #else
  120.     char *command = malloc(6+strlen(filepath)+2+8+2+8+1);
  121.     if(command == NULL) {
  122.       WRITE_GERR(shared_err_block, "NoMem");
  123.       return &shared_err_block; /* fail */
  124.     }
  125. #endif
  126.  
  127.     int old_budge = flex_set_budge(0); /* prevent budge */
  128.  
  129. #ifndef OLD_SCL_STUBS
  130.     snprintf(command, sizeof(command), "CSave %s &%X &%X", filepath, (int)*buffer_anchor, ((int)*buffer_anchor + flex_size(buffer_anchor)));
  131. #else
  132.     sprintf(command, "CSave %s &%X &%X", filepath, (int)*buffer_anchor, ((int)*buffer_anchor + flex_size(buffer_anchor)));
  133. #endif
  134.  
  135.     /* Compress file */
  136.     hourglass_on();
  137.     int err = _kernel_oscli(command);
  138.     hourglass_off();
  139.  
  140. #ifdef OLD_SCL_STUBS
  141.     free(command);
  142. #endif
  143.  
  144.     flex_set_budge(old_budge); /* allow budge */
  145.     if(err == _kernel_ERROR)
  146.       return _kernel_last_oserror(); /* fail */
  147.   }
  148.  
  149.   /* Set file type */
  150.   {
  151.     _kernel_osfile_block inout;
  152.     inout.load = filetype;
  153.     if(_kernel_osfile(18, filepath, &inout) == _kernel_ERROR)
  154.       return _kernel_last_oserror(); /* fail */
  155.   }
  156.  
  157.   return NULL; /* success */
  158. }
  159.